I will be using this Notebook for class demos. To use at home, load Anaconda (https://www.continuum.io/downloads) or WinPython (https://winpython.github.io/)
First let's create a random list using the numpy library.
In [1]:
import numpy as np
nums1 = np.random.randint(1,11, 15)
nums1
Out[1]:
Let's look at what set() does!
In [2]:
set1 = set(nums1)
set1
Out[2]:
Let's create a 2nd list and set.
In [3]:
nums2 = np.random.randint(1,11, 12)
nums2
Out[3]:
In [4]:
set2 = set(nums2)
set2
Out[4]:
...and look at the differences!
In [5]:
set2.difference(set1)
Out[5]:
In [6]:
set1.difference(set2)
Out[6]:
See https://en.wikibooks.org/wiki/Python_Programming/Sets for more information about sets!
In [7]:
# Intersection
set1 & set2
Out[7]:
In [8]:
# Union
set1 | set2
Out[8]:
In [9]:
# Difference
(set1 - set2) | (set2 - set1)
Out[9]:
In [10]:
# Difference method 2
(set1 | set2) - (set1 & set2)
Out[10]:
In [ ]: